﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EasyToHelp.WEBSystem.Models;
using EasyToHelp.Business;
using EasyToHelp.Service;

namespace EasyToHelp.WEBSystem.Controllers
{
    public class HomeController : BaseController
    {
        private readonly ISysMenusService sysMenus;
        public HomeController(ISysMenusService _sysMenus)
        {
            sysMenus = _sysMenus;
        }
        //        <dd data-name="form" class="">
        //          <a href = "javascript:;" > 表单 < span class="layui-nav-more"></span></a>
        //          <dl class="layui-nav-child">
        //            <dd><a lay-href="component/form/element.html">表单元素</a></dd>
        //            <dd><a lay-href="component/form/group.html">表单组合</a></dd>
        //          </dl>
        //        </dd>
        public IActionResult Index()
        {
            var mlist = sysMenus.GetSysMenus();
            if (mlist.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                mlist.ForEach(x =>
                {
                    var ylist = sysMenus.GetChildrenSysMenusList(x.Id);
                    if (ylist.Count > 0)
                    {
                        sb.Append("<li class=\"layui-nav-item\">\r\n");
                        sb.Append("     <a href=\"javascript:;\" lay-tips=\"" + x.text + "\" lay-direction=\"2\">\r\n");
                        sb.Append("         <i class=\"layui-icon layui-icon-home\"></i>\r\n");
                        sb.Append("          <cite>" + x.text + "</cite>\r\n");
                        sb.Append("     </a>\r\n");
                        sb.Append("     <dl class=\"layui-nav-child\">\r\n");
                        ylist.ForEach(y =>
                        {
                            //var zlist = sysMenus.GetChildrenSysMenusList(y.Id);
                            //if (zlist.Count > 0)
                            //{
                            //    sb.Append("         <dd data-name=\"" + y.Id + "\" class=\"\">\r\n");
                            //    sb.Append("              <a lay-href=\"javascript:;\">" + y.text + "</a>\r\n");
                            //    sb.Append("                 <dl class=\"layui-nav-child\">\r\n");
                            //    zlist.ForEach(z =>
                            //    {
                            //        sb.Append("             <dd data-name=\"" + z.Id + "\">\r\n");
                            //        sb.Append("                 <a lay-href=\"" + z.href + "\">" + z.text + "</a>\r\n");
                            //        sb.Append("             </dd>\r\n");
                            //    });
                            //    sb.Append("                 </dl>\r\n");
                            //    sb.Append("         </dd>\r\n");
                            //}
                            //else
                            //{
                            //    sb.Append("         <dd data-name=\"" + y.Id + "\">\r\n");
                            //    sb.Append("              <a lay-href=\"" + y.href + "\">" + y.text + "</a>\r\n");
                            //    sb.Append("         </dd>");
                            //}
                            sb.Append("<dd data-name=\"form\" class=\"\">\r\n");
                            sb.Append("<a href =\"javascript:;\">" + y.text + "<span class=\"layui-nav-more\"></span></a>\r\n");
                            sb.Append("<dl class=\"layui-nav-child\">\r\n");
                            var zlist = sysMenus.GetChildrenSysMenusList(y.Id);
                            if (zlist.Count > 0)
                            {
                                zlist.ForEach(z =>
                                {
                                    sb.Append("<dd><a lay-href=\"" + z.href + "\">" + z.text + "</a></dd>\r\n");
                                });
                            }
                            sb.Append("<dd><a lay-href=\"component/form/element.html\">表单元素</a></dd>\r\n");
                            sb.Append("<dd><a lay-href=\"component/form/group.html\">表单组合</a></dd>\r\n");
                            sb.Append("</dl>\r\n");
                            sb.Append("</dd>\r\n");
                        });
                        sb.Append("     </dl>\r\n");
                        sb.Append("</li>\r\n");
                    }
                    else
                    {
                        sb.Append("<li  class=\"layui-nav-item\">\r\n");
                        sb.Append("     <a href=\"javascript:;\" lay-tips=\"" + x.text + "\" lay-direction=\"" + x.Id + "\">\r\n");
                        sb.Append("         <i class=\"layui-icon layui-icon-home\"></i>\r\n");
                        sb.Append("          <cite>" + x.text + "</cite>\r\n");
                        sb.Append("     </a>\r\n");
                        sb.Append("</li>\r\n");
                    }
                });
                ViewBag.SysMenus = sb.ToString();
            }
            return View(mlist);
        }
        public IActionResult Main()
        {
            return View();
        }
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
